/**
 * 封装界面填写的值到数据对象中
 */
function createDeliveryObjFromContent() {
    var deliveryObj = {};
    deliveryObj.cid = '1';//sessionStorage.getItem('cId');
    deliveryObj.mId = '1234'; //sessionStorage.getItem('mId');
    deliveryObj.openId = '1234';// sessionStorage.getItem('openId');

    var addressArray = $('#address').val().split(' ');
    var province = addressArray[0] || '';
    var city = addressArray[1] || '';
    var country = addressArray[2] || '';

    deliveryObj.province = province;
    deliveryObj.city = city;
    deliveryObj.country = country;
    deliveryObj.address = $('#addressDetail').val();

    deliveryObj.name = $('#name').val();
    deliveryObj.tel = $('#phone').val();

    deliveryObj.amount = $('#amount').val();
    deliveryObj.avgpkg = $('#avgWeight').val();

    return deliveryObj;
}

function createDeliveryObjFromStorage() {
    var deliveryObj = {};
    deliveryObj.mId = sessionStorage.getItem('mId');
    deliveryObj.openId = sessionStorage.getItem('openId');

    deliveryObj.province = sessionStorage.getItem('province');
    deliveryObj.city = sessionStorage.getItem('city');
    deliveryObj.country = sessionStorage.getItem('country');

    deliveryObj.name = sessionStorage.getItem('name');
    deliveryObj.tel = sessionStorage.getItem('tel');

    deliveryObj.amount = sessionStorage.getItem('amount');
    deliveryObj.avgpkg = sessionStorage.getItem('avgpkg');

    return deliveryObj;
}

/**
 * 初始化
 */
!function (window) {
    var deliveryObj = createDeliveryObjFromStorage();

    var $target = $('#address');

    if(deliveryObj.province && deliveryObj.city && deliveryObj.country) {
        $target.citySelect({
            provance: deliveryObj.province,
            city: deliveryObj.city,
            area: deliveryObj.country
        });
    } else {
        $target.citySelect();
    }

    $target.on('click', function (event) {
        event.stopPropagation();
        $target.citySelect('open');
    });

    $target.on('done.ydui.cityselect', function (ret) {
        $(this).val(ret.provance + ' ' + ret.city + ' ' + ret.area);
    });


}(window);

// 提交逻辑
! function (window) {
    $('.sign-btn').on('click', onSignBtnClick);

    function onSignBtnClick() {
        var isValid = validate();
        if (!isValid) {
            return;
        }

        window.submitInfo();
    }

    function validate() {
        var $address = $('#address');
        var $addressDetail = $('#addressDetail');
        var $name = $('#name');
        var $phone = $('#phone');
        var $amount = $('#amount');
        var $avgWeight = $('#avgWeight');

        if (!$address.val() || !trim($address.val())) {
            alert('请选择所在地区');
            return false;
        }

        if (!$addressDetail.val() || !trim($addressDetail.val())) {
            alert('请填写详细地址');
            return false;
        } else if (trim($addressDetail.val()).length < 5) {
            alert('详细地址不能少于5个字');
            return false;
        }

        if (!$name.val() || !trim($name.val())) {
            alert('请填写寄件人姓名');
            return false;
        }

        if (!$phone.val() || !trim($phone.val())) {
            alert('请填写寄件人电话');
            return false;
        } else {
            var phone = trim($phone.val());

            if (!validatePhoneNum(phone)) {
                alert('寄件人电话格式输入有误，请检查');
                return false;
            }
        }

        if (!$amount.val() || !trim($amount.val())) {
            alert('请填写寄件数量');
            return false;
        }

        if (!$avgWeight.val() || !trim($avgWeight.val())) {
            alert('请输入单件平均重量');
            return false;
        }

        return true;
    };

    function trim(str) {
        return str.replace(/(^\s*)|(\s*$)/g, '');
    }

    function validatePhoneNum(phone) {
        if (!(/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/.test(phone))) {
            return false;
        } else {
            return true;
        }
    }

    function validatePhone(phone) {
        if (!(/^1(3|4|5|7|8)\d{9}$/.test(phone))) {
            return false;
        } else {
            return true;
        }
    }

    function validateTel(tel) {
        if (!/^(\(\d{3,4}\)|\d{3,4}-|\s)?\d{7,14}$/.test(tel)) {
            return false;
        } else {
            return true;
        }
    }
}(window);

/**
 * 网络请求
 */
! function (window) {
    function submitInfo() {
        $('#footer').addClass('footer-disabled');

        $.ajax({
            url: "http://10.2.4.130:8888/services/regInfo/insert/",
            type: "post",
            contentType:'application/json',
            dataType: 'json',
            data: JSON.stringify(createDeliveryObjFromContent()),
            xhrFields: {  
                'Access-Control-Allow-Origin': '*'
            },
            success: function (data) {
                window.href = '../../main/index.html';
                $('#footer').removeClass('footer-disabled');
            },
            error: function (msg) {
                window.href = '../../main/index.html';
                $('#footer').removeClass('footer-disabled');
            }
        });
    }

    window.submitInfo = submitInfo;
}(window);